﻿/******************************************************************************
* File description: ControlPannel.qml
* Function: 实验控制模块（实验开始、暂停、剩余时间那一行）
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12

import "../components"
import "../promptBox"

Rectangle{

    id: root

    height: 120*dp
    width: parent.width
    radius: 3*dp
    color: "#F7F7FA"

    property string runningTime: {
        if(gExpIsRunning === 0){
            return "--:--:--"
        }
        return funcTools.second2Time(gStateExp.expRunningTime)
    }

    property string remainTime: {
        if(gExpIsRunning === 0){
            return "--:--:--"
        }else{
            return funcTools.second2Time(gStateExp.expRemainTime)
        }
    }
    property string stopTime: {
        if(gExpIsRunning === 1){
            return new Date(new Date().getTime() + 1000*gStateExp.expRemainTime).toLocaleString(Qt.locale("de_DE"), "HH:mm:ss")
        }else{
            return "--:--:--"
        }
    }
    property string step: gStateExp.expStep + "/" + gStateExp.expStepCount



    CustomPopupBoxWarning{
        id: box_opRun
        box_text: "确认启动实验【"+gStateExp.selectedExpName+"】吗?" + (gStateExp.selectedExpType===0||gStateExp.selectedExpType===4 ? " 请确保耗材已正确设置":"")
        onAccepted: {
            box_opStartExp.startWait01()
            box_opStartExp.show()
            CGetMessageInterface.setExpBaseInfo(gStateExp.getExpUserInfo())        // 设置用户信息
            CommunicationMethod.startExperiment()                                  // 给设备发送运行信号
        }
    }
    CustomPopupBoxWarning{
        id: box_opStop
        box_text: "确认停止实验吗?"
        onAccepted: {
            box_opEndExp.startWait01()
            box_opEndExp.show()
            CommunicationMethod.operationExperiment(2)
        }
    }
    CustomPopupBoxWarning{
        id: box_opPause
        box_text: "确认暂停吗?"
        onAccepted: {
            box_opPauseExp.startWait01()
            box_opPauseExp.show()
            CommunicationMethod.operationExperiment(3)
        }
    }
    function continueExp(){
        box_opContinueExp.startWait01()
        box_opContinueExp.show()
        CommunicationMethod.operationExperiment(4)
    }

    CustomPopupBoxWarning{
        id: box_opContinue
        box_text: "确认继续实验吗?"
        onAccepted: {
            if(gPauseNeedPassword){
                box_pausePassword.show()
            }else if(gStateExp.expPauseType === 2){     // 活检的暂停，请输入浓度值
                box_pauseConc.show()
            }else{
                continueExp()
            }
        }
    }
    BoxPausePassword{
        id: box_pausePassword
        onAccepted: {
            continueExp()
        }
    }
    BoxPauseConc{
        id: box_pauseConc
        onAccepted: {
            continueExp()
        }
    }

    CustomPopupBoxWarning{
        id: box_opReset
        box_text: "确认复位设备到【"+gStateExp.selectedExpName+"】实验位置吗?"
        onAccepted: {
            box_resetting.infoState = 0
            // 全部复位
            if(gStateExp.selectedExpType === 0){
                CAnimationShifting.reset()
                CAnimationPractice.reset()
                CAnimationPurify.reset()
                CMaintenance.reset("1", "0", gStateExp.consumableName)
            }
            // 放靶
            else if(gStateExp.selectedExpType === 1){
                CAnimationShifting.reset()
                CMaintenance.reset("1", "10", gStateExp.consumableName)
            }
            // 打靶
            else if(gStateExp.selectedExpType === 2){
                CAnimationPractice.reset()
                CMaintenance.reset("1", "11", gStateExp.consumableName)
            }
            // 转靶
            else if(gStateExp.selectedExpType === 3){
                CAnimationShifting.reset()
                CMaintenance.reset("1", "10", gStateExp.consumableName)
            }
            // 纯化
            else if(gStateExp.selectedExpType === 4){
                CAnimationPurify.reset()
                CMaintenance.reset("1", "8", gStateExp.consumableName)
            }
            box_resetting.show()        // 显示等待复位的弹窗
        }
    }
    CustomPopupBoxWarning{
        id: box_opResetOrigin
        box_text: "确认复位设备到原点位置吗?"
        onAccepted: {
            box_resetting.infoState = 0
            CMaintenance.reset("0", "8", "")
            CAnimationShifting.reset()
            CAnimationPractice.reset()
            CAnimationPurify.reset()
            box_resetting.show()        // 显示等待复位的弹窗
        }
    }

    // 点击运行
    function onButtonRunningClicked(){
        if(gConnectStatus === false){
            box_error_common.box_text = "请先连接设备"
            box_error_common.show()
            return
        }
//        if(!gStateExp.checkIsConfirm()){
//            box_error_common.box_text = "请先确认实验信息后再启动实验"
//            box_error_common.show()
//            return
//        }
        if(gStateExp.selectedExpName === ""){
            box_error_common.box_text = "请先选择实验"
            box_error_common.show()
            return
        }
//        if(CGetMessageInterface.checkNeedReset(gStateExp.selectedExpType, gStateExp.consumableName)){
//            box_error_common.box_text = "请先确认耗材"
//            box_error_common.show()
//            return
//        }
        CGetMessageInterface.setExpInfo(gStateExp.selectedExpType, gStateExp.selectedExpName, gStateExp.consumableName)

        // 耗材编码校验
        if(gAppModule === 0 || gAppModule === 2){       // 模块屏蔽，有纯化模块时才进行耗材编码校验
            var checkCodeRes = CBarCode.isValidCode(gStateExp.consumableCode)
            if(checkCodeRes !== "" && isCheckConsumableCode){
                if(gStateExp.mainMethod !== "" || gPageIdx===4){
                    box_error_common.box_text = checkCodeRes
                    box_error_common.show()
                    if(gPageIdx !== 4){     // 跳转到主页提醒用户填写耗材编码
                        gPageIdx = 0
                    }
                    return
                }
            }
        }
        // 后端的不知道是什么的校验
        var checkResult = CommunicationMethod.checkExpRunningRule()
        if(checkResult !== ""){
            box_error_common.box_text = checkResult
            box_error_common.show()
            return
        }
        box_opRun.show()
    }
    // 点击停止
    function onButtonStopClicked(){
        box_opStop.show()
    }
    // 点击暂停
    function onButtonPauseClicked(){
        box_opPause.show()
    }
    // 点击继续
    function onButtonContinueClicked(){
        box_opContinue.show()
    }
    // 点击复位
    function onButtonResetClicked(){
        if(gConnectStatus === false){
            box_error_common.box_text = "请先连接设备"
            box_error_common.show()
            return
        }
        if(gStateExp.selectedExpName === ""){
            box_error_common.box_text = "请先选择实验"
            box_error_common.show()
            return
        }
//        // 校验是否点击确认
//        if(!gStateExp.checkIsConfirm()){
//            box_error_common.box_text = "请先确认实验信息后再复位实验"
//            box_error_common.show()
//            return
//        }
        box_opReset.show()
    }
    // 点击复位原点
    function onButtonResetOriginClicked(){

        if(gConnectStatus === false){
            box_error_common.box_text = "请先连接设备"
            box_error_common.show()
            return
        }
        box_opResetOrigin.show()
    }

    // 左边的按钮区 ==========================================================
    Row{
        x: 20*dp
        spacing: 20*dp
        height: parent.height
        ControlPannelButton2{
            title: {
                if(gExpIsRunning === 0)        return "运行"
                else if(gExpIsRunning === 1)   return "停止"
                else                           return "运行"
            }
            fontColor: (!enabled) ? "#222" : (title==="运行" ? "#FFF" : "#222")
            color: (!enabled) ? "white" : (title==="运行" ? "#56BD6C" : "white")
            icon: title==="运行" ? "qrc:/picture/controlPannelButton/run.png" : "qrc:/picture/controlPannelButton/stop.png"
            icon_disabled: title==="运行" ? "qrc:/picture/controlPannelButton/run_d.png" : "qrc:/picture/controlPannelButton/stop_d.png"
            anchors.verticalCenter: parent.verticalCenter
            enabled: gConnectStatus && gExpIsExecOperate === 0
            onClicked: {
                if(title === "运行"){
                    onButtonRunningClicked()
                }else{
                    onButtonStopClicked()
                }
            }
            onIsHoveredChanged: {
                if(isHovered){
                    if(!enabled)   toolTip_startAndStop.visible = true
                }else{
                    toolTip_startAndStop.visible = false
                }
            }
            CustomTooltip{
                id: toolTip_startAndStop
                text: {
                    if(gExpIsRunning && gExpIsFailurePause)    return "实验故障，请前往故障页面进行处理"
                    if(gConnectStatus === false)    return "设备未连接"
                    if(gExpIsRunning === 0)    return "点击以启动实验"
                    if(gExpIsRunning === 1)    return "点击以停止实验"
                    return ""
                }
            }
        }
        ControlPannelButton2{
            title: {
                if(gExpIsPause === 0)        return "暂停"
                else if(gExpIsPause === 1)   return "继续"
                else                        return "暂停"
            }
            icon: title==="暂停" ? "qrc:/picture/controlPannelButton/pause.png" : "qrc:/picture/controlPannelButton/continue.png"
            icon_disabled: title==="暂停" ? "qrc:/picture/controlPannelButton/pause_d.png" : "qrc:/picture/controlPannelButton/continue_d.png"
            anchors.verticalCenter: parent.verticalCenter
            enabled: gConnectStatus && gExpIsRunning === 1 && gExpIsFailurePause === 0 && gExpIsExecOperate === 0
            onClicked: {
                if(title === "暂停"){
                    onButtonPauseClicked()
                }else{
                    onButtonContinueClicked()
                }
            }
            onIsHoveredChanged: {
                if(isHovered){
                    if(!enabled)   toolTip_pauseAndContinue.visible = true
                }else{
                    toolTip_pauseAndContinue.visible = false
                }
            }
            CustomTooltip{
                id: toolTip_pauseAndContinue
                text: {
                    if(gExpIsRunning && gExpIsFailurePause)    return "实验故障，请前往故障页面进行处理"
                    if(gConnectStatus === false)    return "设备未连接"
                    if(gExpIsRunning === 0)       return "当前未进行实验"
                    if(gExpIsExecOperate === 1)     return "设备正在执行操作"
                    return ""
                }
            }
        }
        ControlPannelButton2{
            title: "复位"
            icon: "qrc:/picture/controlPannelButton/reset.png"
            icon_disabled: "qrc:/picture/controlPannelButton/reset_d.png"
            anchors.verticalCenter: parent.verticalCenter
            enabled: gConnectStatus && gExpIsRunning === 0 && gExpIsExecOperate === 0
            onClicked: {
                onButtonResetClicked()
            }
            onIsHoveredChanged: {
                if(isHovered){
                    if(!enabled)   toolTip_reset.visible = true
                }else{
                    toolTip_reset.visible = false
                }
            }
            CustomTooltip{
                id: toolTip_reset
                text: {
                    if(gConnectStatus === false)          return "设备未连接"
                    else if(gExpIsRunning === 1)          return "实验正在运行中，无法进行复位"
                    else if(gExpIsExecOperate === 1)      return "设备正在执行操作"
                    else                                  return "点击以复位设备"
                }
            }
        }
        ControlPannelButton2{
            title: "复位原点"
            visible: gAppModule === 2
            icon: "qrc:/picture/controlPannelButton/resetOrigin.png"
            icon_disabled: "qrc:/picture/controlPannelButton/resetOrigin_d.png"
            anchors.verticalCenter: parent.verticalCenter
            enabled: gConnectStatus && gExpIsRunning === 0 && gExpIsExecOperate === 0
            onClicked: {
                onButtonResetOriginClicked()
            }
            onIsHoveredChanged: {
                if(isHovered){
                    if(!enabled)   toolTip_reset2.visible = true
                }else{
                    toolTip_reset2.visible = false
                }
            }
            CustomTooltip{
                id: toolTip_reset2
                text: {
                    if(gConnectStatus === false)          return "设备未连接"
                    else if(gExpIsRunning === 1)          return "实验正在运行中，无法进行复位"
                    else if(gExpIsExecOperate === 1)      return "设备正在执行操作"
                    else                                  return "点击以复位设备"
                }
            }
        }
    }


    // 中间的信息区 ============================================================
    Rectangle{      // 多出来的白色背景
        height: 90*dp
        width: row_info.width + 40*dp
        x: row_info.x - 20*dp
        radius: 12*dp
        anchors.verticalCenter: parent.verticalCenter
    }
    Row{
        id: row_info
        x: gAppModule===2 ? (parent.width - width - 180*dp) : (parent.width - width - 40*dp)
        spacing: 20*dp
        height: parent.height
        ControlPannelInfo2{
            title: "剩余时间"
            content: remainTime
            color: "#F7F6FF"
//            icon: "qrc:/picture/controlPannelButton/remainTime.png"
            anchors.verticalCenter: parent.verticalCenter
        }
        Rectangle{ height: 60*dp; width: 1; color: "#CCC"; anchors.verticalCenter: parent.verticalCenter }
        ControlPannelInfo2{
            title: "预结束时间"
            content: stopTime
            color: "#F5F9FF"
//            icon: "qrc:/picture/controlPannelButton/finishTime.png"
            anchors.verticalCenter: parent.verticalCenter
        }
        Rectangle{ height: 60*dp; width: 1; color: "#CCC"; anchors.verticalCenter: parent.verticalCenter }
        ControlPannelInfo2{
            title: "运行时间"
            color: "#F5F9FF"
            content: runningTime
            anchors.verticalCenter: parent.verticalCenter
        }
        Rectangle{ height: 60*dp; width: 1; color: "#CCC"; anchors.verticalCenter: parent.verticalCenter }
        ControlPannelInfo2{
            title: "步骤"
            content: step
            color: "#F4FDF9"
//            icon: "qrc:/picture/controlPannelButton/step.png"
            anchors.verticalCenter: parent.verticalCenter
        }

    }


    // 右面的两个按钮
    Column{
        visible: gAppModule === 2
        x: parent.width - 120*dp - 20*dp
        spacing: 10*dp
        anchors.verticalCenter: parent.verticalCenter
        CustomButton{
            defaultMode1: true
            title: "打印报告"
            onClicked: {
                box_printer.close()
                box_printer.show()
            }
        }CustomButton{
            defaultMode1: true
            enabled: gExpIsRunning === 0 || gExpIsFailurePause
            title: "系统设置"
            onClicked: {
                preset.show()
            }
        }
    }


}
